<template>
	<view class="emailDetail">
		<view v-if="isresume" class="emailDetail_manBody">
			<uni-section :title="currentItem.msg.userName+'投递了'+currentItem.toStr" type="line">
				<view class="example-name f16">
					{{currentItem.msg.resumeName }}
				</view>
			</uni-section>
			<uni-section title="姓名:" type="line">
				<view class="example-name f16">
					{{currentItem.msg.userName}}
				</view>
			</uni-section>
			<uni-section title="联系:" type="line">
				<view class="example-name f16">
					{{currentItem.msg.connect}}
				</view>
			</uni-section>
			<uni-section title="学院:" type="line">
				<view class="example-name f16">
					{{currentItem.msg.institute}}
				</view>
			</uni-section>
			<uni-section title="专业:" type="line">
				<view class="example-name f16">
					{{currentItem.msg.major}}
				</view>
			</uni-section>
			<uni-section title="意向:" type="line">
				<view class="example-name f16">
					{{currentItem.msg.intention}}
				</view>
			</uni-section>
			<uni-section title="长处:" type="line">
				<view class="example-name f16">
					{{currentItem.msg.forte}}
				</view>
			</uni-section>
			<uni-section title="是否通过" type="line">
				<view class="example-name f16" v-if="!isOperator">
					<uni-data-checkbox v-model="isAccept" :localdata="options" />
				</view>
				<view class="example-name f16" v-else>
					<text>{{isAccept == 1? '已通过' :'已拒绝'}}</text>
				</view>
			</uni-section>
			<view class="button" v-if="!isOperator">
				<view type="primary" class="button_item f14 margin-right16" @click="confirm">提交</view>
			</view>
			<uni-popup ref="alertDialog" type="dialog">
				<uni-popup-dialog cancelText="算了" confirmText="确定" title="温馨提示"
					:content="isAccept == 1 ? '确定要通过申请吗': '确定要拒绝申请吗'" @confirm="dialogConfirm" @close="dialogClose">
				</uni-popup-dialog>
			</uni-popup>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				currentItem: null,
				isresume: false,
				isOperator: false,
				isAccept: 3, //没操作
				formData: {
					teamId: 0
				},
				options: [{
					text: '通过',
					value: 1
				}, {
					text: '不通过',
					value: 0
				}]
			}
		},
		methods: {


			async dialogConfirm() {
				try {
					const data = {
						teamId: String(this.currentItem.msg.teamId),
						joinUserId: String(this.currentItem.msg.userId),
					}
					const result = await this.$mjcrequest.request({
						url: this.isAccept == 1 ? this.$api.accept2JoinTeam : this.$api.reject2JoinTeam,
						header: this.$aplsjsonheader,
						data: JSON.stringify(data),
						isToast: true
					});
					this.isOperator = true
					this.isAccept = this.isAccept
					if (this.isAccept == 0) {
						this.$errMessageToast('已拒绝')
					}
				} catch (e) {
					this.$errMessageToast(e)
					//TODO handle the exception
				}
			},
			async checkJoin() {
				console.log(this.currentItem);

				try {
					const data = {
						teamId: String(this.currentItem.msg.teamId),
						joinUserId: String(this.currentItem.msg.userId)
					}

					const result = await this.$mjcrequest.request({
						url: this.$api.checkJoin,
						header: this.$aplsjsonheader,
						data: JSON.stringify(data),
						isNotLoading: true
					});
					this.isAccept = result
					if (result != 3) {
						this.isOperator = true
					}
				} catch (e) {
					this.$errMessageToast(e)
					//TODO handle the exception
				}
			},
			dialogToggle() {
				this.$refs.alertDialog.open()
			},
			vaildType() {
				console.log(this.isAccept);
				if (this.isAccept == 3) {
					throw new Error('请确定是否通过请求者')
				}
				return true
			},
			async confirm() {
				console.log('confirm');
				try {
					console.log('confirm');
					if (this.vaildType()) {
						this.dialogToggle()
					}
				} catch (e) {
					this.$errMessageToast(e.message || e)
					//TODO handle the exception
				}
			},
			async sendRead() {
				try {
					const data = {
						msgId: String(this.currentItem.msgId)
					}
					const result = await this.$mjcrequest.request({
						url: this.$api.Read,
						header: this.$aplsjsonheader,
						data: JSON.stringify(data),
						isNotLoading: true
					});
				} catch (e) {
					this.$errMessageToast(e)
					//TODO handle the exception
				}
			}
		},
		mounted() {

		},
		created() {
			this.$eventBus.$on('temEmailMsgSend', (item, index) => {

				let newItem = {}
				if (item.type == 'reSume') {
					this.isresume = true
					//简历申请
					newItem = {
						...item,
						msg: JSON.parse(item.msg)
					}
				} else {
					newItem = {
						...item
					}
				}
				this.currentItem = newItem
				if (item.type == 'reSume') {
					console.log(';checkJoin');
					this.checkJoin()
				}

				console.log(newItem, 22);
				if (item.isRead != 1) {
					//已读
					this.sendRead()
				}



			})
			this.$eventBus.$emit('temEmailMsg')

		},
		destroyed() {
			this.$eventBus.$off('temEmailMsgSend')
		}
	}
</script>

<style lang="less" scoped>
	.emailDetail {
		height: auto;
		padding: 0 15rpx;
		background-color: #fff;
		padding-bottom: 20rpx;

		&_manBody {}

		.example-name {
			padding: 5px 10px;
		}

		.checklist-group {
			display: flex;
			justify-content: center;
		}

		.button {
			display: flex;
			background-color: #fff;
			padding-top: 15rpx;
			justify-content: center;

			&_item {
				width: auto;
				background-color: #2ebef3;
				color: #fff;
				padding: 15rpx 35rpx;
				border-radius: 15rpx;
			}
		}
	}
</style>
